package com.hfb.mashibing.alip8.diamondsquarealgorithm.class10;

import java.util.HashSet;
import java.util.Stack;

public class Code02_DFS {

    /**
     * 深度优先遍历:
     * 沿着一条路走到头，再回头
     */
    public static void dfs(Node node) {
        if (null==node){
            return;
        }
        // 记录每次深度遍历的路径
        Stack<Node> stack = new Stack<Node>(){{
            add(node);
        }};
        HashSet<Node> set = new HashSet<Node>(){{
            add(node);
        }};
        System.err.println(node.value);
        while (!stack.isEmpty()) {
            Node cur = stack.pop(); // 栈弹出值
            for(Node next:cur.nexts) {
                if (!set.contains(next)) {
                    set.add(next);
                    stack.push(cur);
                    stack.push(next);
                    System.err.println(next.value);
                    break;
                }
            }
        }
    }

}
